import java.io.*;
import java.util.*;

class crypto_02 {

    private static int hex2dec(char hex) {
        if ('0' <= hex && hex <= '9') {
            return (hex - '0');
        } else if (hex >= 'A' && hex <= 'Z') {
            return (hex - 'A'  + 10);
        }

        return (hex - 'a' + 10);
    }

    private static char dec2hex(int dec) {
        if (dec > 9)
            return ((char)((int)'a' + (dec - 10)));

        return ((char)((int)'0' + dec));
    }

    private static String readtext(String file) throws IOException {
        FileReader fr = null;
        String line = null;

        try {
            fr = new FileReader (file);
            System.out.println("Input file: " + file);
        } catch(FileNotFoundException e) {
            System.out.println("Unable to open file:" + file);
            System.out.println("ERROR = " + e.getMessage());
        }

        BufferedReader reader = new BufferedReader(fr);

        while (reader.ready())
            line = reader.readLine();

        reader.close();
        return (line);
    }

    public static void main(String[] args) throws IOException {
        String plaintext = null;
        plaintext = readtext(args[0]);
        System.out.println("Plaintext: " + plaintext);
        System.out.println("Plaintext length = " + plaintext.length());

        String key = null;
        key = readtext(args[1]);
        System.out.println("Key: " + key);
        System.out.println("Key length = " + key.length());

        if (plaintext.length() != key.length()) {
            System.out.println("ERROR: Mismatch in length of plaintext and key.");
            Runtime.getRuntime().exit(-1);
        }

        StringBuilder ciphertext = new StringBuilder();

        for (int idx = 0; idx < plaintext.length(); idx++) {
            int p = hex2dec(plaintext.charAt(idx));
            int k = hex2dec(key.charAt(idx));
            int c = p ^ k;
            ciphertext.append(dec2hex(c));
        }
        System.out.println( "Ciphertext: " + ciphertext);
    }
}
